package com.draven.management.controller;

import com.draven.common.BaseController;
import com.draven.entity.Module;
import com.draven.model.Tree;
import com.draven.service.ModuleService;
import com.draven.utils.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Draven on 2017/4/16.
 * E-mail:draven1122@163.com
 */
@Controller("management.moduleController")
@RequestMapping("/management/module")
public class ModuleController extends BaseController {
    @Autowired
    private ModuleService moduleService;

    @RequestMapping("")
    public String main() {
        return "/management/module/main";
    }

    /**
     * 获取模块树
     *
     * @return
     */
    @RequestMapping("/tree")
    @ResponseBody
    public String tree() {
        List<Module> modules = null;
        List<Tree> trees = new ArrayList<Tree>();
        try {
            modules = moduleService.listModulesByParentId("0");
            Tree tree = new Tree();
            tree.setId("0");
            tree.setText("一级模块");
            trees.add(tree);
            for (Module module : modules) {
                Tree tree1 = new Tree();
                tree1.setId(module.getId());
                tree1.setText(module.getModuleName());
                trees.add(tree1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return listToJsonArrayStr(trees);
        }
    }

    /**
     * 获取模块树
     *
     * @return
     */
    @RequestMapping("/list/{id}")
    @ResponseBody
    public String list(@PathVariable("id") String parentId) {
        List<Module> modules = null;
        try {
            modules = moduleService.listModulesByParentId(parentId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return listToJsonArrayStr(modules);
        }
    }

    @RequestMapping("/save")
    public ModelAndView save(Module module, String type) {
        if ("add".equals(type)) {
            try {
                module.setId(UUIDGenerator.nextId());
                moduleService.addModule(module);
                return ajaxDoneSuccess("增加模块成功");
            } catch (Exception e) {
                e.printStackTrace();
                return ajaxDoneError("新增模块失败");
            }
        } else {
            try {
                moduleService.updateModuleSelective(module);
                return ajaxDoneSuccess("修改模块成功");
            } catch (Exception e) {
                e.printStackTrace();
                return ajaxDoneError("修改模块失败");
            }
        }
    }
    @RequestMapping("/remove/{id}")
    public ModelAndView remove(@PathVariable("id")String id){
        try {
            moduleService.removeModule(id);
            return ajaxDoneSuccess("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxDoneError("删除失败，发生系统失败");
        }
    }

}
